package gui.reports.supply;

import java.io.File;
import java.io.IOException;

import javax.swing.JOptionPane;

import core.CoreFacade;
import gui.common.*;

/**
 * Controller class for the N-month supply report view.
 */
	public class SupplyReportController extends Controller implements
		ISupplyReportController {
/*---	STUDENT-INCLUDE-BEGIN

	/**
	 * Constructor.
	 * 
	 * @param view Reference to the N-month supply report view
	 */	
	public SupplyReportController(IView view) 
	{
		super(view);
		
		construct();
	}

	//
	// Controller overrides
	//
	
	/**
	 * Returns a reference to the view for this controller.
	 * 
	 * {@pre None}
	 * 
	 * {@post Returns a reference to the view for this controller.}
	 */
	@Override
	protected ISupplyReportView getView() {
		return (ISupplyReportView)super.getView();
	}

	/**
	 * Sets the enable/disable state of all components in the controller's view.
	 * A component should be enabled only if the user is currently
	 * allowed to interact with that component.
	 * 
	 * {@pre None}
	 * 
	 * {@post The enable/disable state of all components in the controller's view
	 * have been set appropriately.}
	 */
	@Override
	protected void enableComponents() 
	{
		int numMonths = 0;
		try
		{
			numMonths = Integer.parseInt(getView().getMonths());
		}
		catch(Exception e)
		{
			numMonths = 0;
		}
		
		if ((numMonths >= 1) && (numMonths <= 100))
		{
			//enable Ok
			getView().enableOK(true);
		}
		else
		{
			//disable ok
			getView().enableOK(false);
		}
	}

	/**
	 * Loads data into the controller's view.
	 * 
	 *  {@pre None}
	 *  
	 *  {@post The controller has loaded data into its view}
	 */
	@Override
	protected void loadValues() 
	{
		getView().setMonths("3");
	}

	//
	// IExpiredReportController overrides
	//

	/**
	 * This method is called when any of the fields in the
	 * N-month supply report view is changed by the user.
	 */
	@Override
	public void valuesChanged() 
	{
		enableComponents();
	}
	
	/**
	 * This method is called when the user clicks the "OK"
	 * button in the N-month supply report view.
	 */
	@Override
	public void display() 
	{	
		//create the report and display it.
		
		String file = CoreFacade.getInstance().createThreeMonthSupply(
				Integer.parseInt(getView().getMonths()), getView().getFormat().toString());
		
		//Open the file
		System.out.println("filename = " + file);
		
		if (file.length() == 0) return;
		
		try 
		{
			java.awt.Desktop.getDesktop().open(new File(file));
		} 
		catch (IOException e) 
		{
			e.printStackTrace();
		}
		
	}

}

